import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class JDBCFirstProg {

	public static void main(String[] args) {
		// JDBC API Objects Declaration
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		// Username and Password for login the database
		String strUser = "";
		String strPass = "";
		
		// Register the Driver
		try
		{
			// Another way is "new sun.jdbc.odbc.JdbcOdbcDriver()"
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		}	
		catch (ClassNotFoundException e)
		{
			// Exception handler for Class.forName(...)
			System.err.println("Error! " + e.getMessage());
			System.err.println("Please install JDBC-ODBC Driver first.");
			return;
		}
		
		// Main Process of retrieving data with JDBC
		try
		{
			// Create Connection
			// ODBC name : MyBank moust br define
			conn = DriverManager.getConnection("jdbc:odbc:MyBank", strUser, strPass);
			
			// Create SQL Statement
			String strSQL = "SELECT * FROM accounts";
			stmt = conn.createStatement();
			
			// Return the Results
			rs = stmt.executeQuery(strSQL);
			// Retrieve Data
			while (rs.next())
			{
				System.out.print(rs.getInt("actID") + " ");
				System.out.print(rs.getString("actRealName") + " ");
				System.out.print(rs.getString("actEMail") + " ");
				System.out.print(rs.getString("actLogin") + " ");
				System.out.print(rs.getString("actPassword") + " ");
				System.out.print(rs.getFloat("actBalance") + " ");
				System.out.println(rs.getDate("actLastLogin"));
			}
		}
		catch (SQLException e)
		{
			System.err.println("SQL Error! " + e.getMessage());
			e.printStackTrace(System.err);
		}
		finally
		{
			// Close all resource on server and release memory at client
			try
			{
				rs.close();
				stmt.close();
				conn.close();
			}
			catch (SQLException e)
			{
				System.err.println("SQL Error! " + e.getMessage());
				e.printStackTrace(System.err);
			}
			rs = null;
			stmt = null;
			conn = null;
		}
	}

}
